/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package di.persistence.entities;

import di.persistence.entities.interfaces.IEntity;
import di.persistence.object.UserBO;
import di.server.context.AppContext;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import javax.persistence.*;
import org.apache.log4j.Level;
import org.apache.log4j.Priority;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 *
 * @author Kilian Ito
 */
@Entity
//@Inheritance(strategy = InheritanceType.JOINED)
@Table(uniqueConstraints={
    @UniqueConstraint(columnNames={"login"}),
    @UniqueConstraint(columnNames={"email"})
})
public class User implements IEntity {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    protected String firstName;
    protected String lastName;
    protected String password;
    protected String login;
    protected String email;
    
    @Fetch(FetchMode.SELECT)
    @OneToMany(cascade={CascadeType.REFRESH}, fetch=FetchType.LAZY)
    protected List<UserType> types;
    
    @OneToOne(cascade={CascadeType.ALL})
    protected SmartPhone phone;

    
    
    public User() {
    }
    
    
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public SmartPhone getPhone() {
        return phone;
    }

    public void setPhone(SmartPhone phone) {
        this.phone = phone;
    }

    public List<UserType> getTypes() {
        types=AppContext.getBean(UserBO.class).getTypes(this);
        if (types == null)
            types = new ArrayList<>();
        return types;
    }

    public void setType(List<UserType> types) {
        this.types = types;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }
    
    
    
    
    
    
    
    
    
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getFirstName() + " " + getLastName() + " (" + getId() + ")";
    }
    
}
